﻿//-----------------------------------------------------------------------
// <copyright file="UserNamePasswordSaveAndCloseCommand.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 
namespace ChatBox.UI.Windows.UserNamePassword
{
    using ChatBox.DomainObjects.Settings;
    using ChatBox.Interfaces;
    using ChatBox.UI.Commands;

    /// <summary>
    /// A command to save and close the user name and password dialog
    /// </summary>
    public class UserNamePasswordSaveAndCloseCommand : GenericCommandBase<UserNamePasswordViewModel>
    {
                /// <summary>
        /// The source of server settings
        /// </summary>
        private readonly IServerSettingsRepository serverSettingsRepository;

        /// <summary>
        /// Initializes a new instance of the UserNamePasswordSaveAndCloseCommand class
        /// </summary>
        /// <param name="serverSettingsRepository">The source of server settings</param>
        public UserNamePasswordSaveAndCloseCommand(IServerSettingsRepository serverSettingsRepository)
        {
            this.serverSettingsRepository = serverSettingsRepository;
        }

        /// <summary>
        /// Execute the command
        /// </summary>
        /// <param name="value">the data for the command</param>
        public override void ExecuteOnValue(UserNamePasswordViewModel value)
        {
            string passwordOnWindow = ((UserNamePasswordPrompt)value.Window).Password;

            ServerSettingsData settingsData = this.serverSettingsRepository.ReadSettings();
            if ((settingsData.UserName != value.UserName) || (settingsData.Password != passwordOnWindow))
            {
                settingsData.UserName = value.UserName;
                settingsData.Password = passwordOnWindow;

                this.serverSettingsRepository.SaveSettings(settingsData);
            }

            value.Window.Close();
        }

        /// <summary>
        /// Hack to stop it being falsely disabled
        /// </summary>
        /// <param name="parameter">the object to test</param>
        /// <returns>always true</returns>
        public override bool CanExecute(object parameter)
        {
            return true;
        }
    }
}
